#include "MEMSDataSaver.h"

#include "qdatetime.h"

#include "FileWriter.h"
#include "LogSaver.h"

MEMSDataSaver::MEMSDataSaver(QObject* parent) :QObject(parent)
{
	qRegisterMetaType<QHash<int, QString>>("QHash<int,QString>");
	for (int dataGroupIndex = 0; dataGroupIndex < dataGroupCount; ++dataGroupIndex)
	{
		setFilePath(dataGroupIndex, ("mEMSData_group_" + QString::number(dataGroupIndex + 1) + ".txt"));
	}
}
MEMSDataSaver::~MEMSDataSaver()
{
	disconnect();
}
void MEMSDataSaver::slot_saveData(int dataGroupIndex, QVector<float> data, MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol)
{
	//暂时不考虑数据title的问题
	writeToFile_data(dataGroupIndex, data, dataCollectionProtocol);
}
void MEMSDataSaver::writeToFile_title(int dataGroupIndex, MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol)
{
	QString title_string;
	if (filePaths.contains(dataGroupIndex))
	{
		switch (dataCollectionProtocol)
		{
		case MEMSDataHandler::Protocol_SingleTemperature:
			title_string.append(title_protocol_singleTemperature);
			break;
		case MEMSDataHandler::Protocol_DoubleTemperature:
			title_string.append(title_protocol_doubleTemperature);
			break;
		default:
			break;
		}
		title_string.append(newLine);
		fileWriter->saveAsFile<MEMSDataSaver>(filePaths[dataGroupIndex], title_string.toUtf8(), FileWriter::WriteToFile, this, &MEMSDataSaver::signal_saveDataFileSuccessful, &MEMSDataSaver::signal_saveDataFileFailed);
		setFirstWrite(dataGroupIndex, false);
	}
}
void MEMSDataSaver::writeToFile_data(int dataGroupIndex, QVector<float> data, MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol)
{
	QString data_string;
	switch (dataCollectionProtocol)
	{
	case MEMSDataHandler::Protocol_SingleTemperature:
		data_string.append(QString::number(data[6], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[3], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[4], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[5], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[0], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[1], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[2], 'f', accuracy) + "\t");
		logSaver->saveLog(QStringLiteral("%1：保存第%2组数据（单温度协议）：%3").arg(objectName()).arg(dataGroupIndex + 1).arg(data_string), LogSaver::Debug);
		break;
	case MEMSDataHandler::Protocol_DoubleTemperature:
		data_string.append(QString::number(data[7], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[3], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[4], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[5], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[6], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[0], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[1], 'f', accuracy) + "\t");
		data_string.append(QString::number(data[2], 'f', accuracy) + "\t");
		logSaver->saveLog(QStringLiteral("%1：保存第%2组数据（双温度协议）：%3").arg(objectName()).arg(dataGroupIndex + 1).arg(data_string), LogSaver::Debug);
		break;
	default:
		break;
	}
	emit signal_mEMSDataToSave(dataGroupIndex, data_string);
	data_string.append(newLine);
	fileWriter->saveAsFile<MEMSDataSaver>(filePaths[dataGroupIndex], data_string.toUtf8(), FileWriter::AppendToFile, this, &MEMSDataSaver::signal_saveDataFileSuccessful, &MEMSDataSaver::signal_saveDataFileFailed);
}
bool MEMSDataSaver::isFirstWrite(int dataGroupIndex)
{
	return firstWrite.value(dataGroupIndex);
}

void MEMSDataSaver::setFirstWrite(int dataGroupIndex, bool firstWriteState)
{
	if (filePaths.contains(dataGroupIndex))
	{
		firstWrite.insert(dataGroupIndex, firstWriteState);
	}
	else
	{
		firstWrite.remove(dataGroupIndex);
	}
}
QList<QString> MEMSDataSaver::getFilePaths()
{
	QList<QString> filePathList;
	for (int i=0;i< filePaths.size();++i)
	{
		filePathList.append(filePaths.value(i));
	}
	return filePathList;
}
QString MEMSDataSaver::getFilePath(int dataGroupIndex)
{
	return filePaths[dataGroupIndex];
}
void MEMSDataSaver::setFilePath(int dataGroupIndex, QString filePath)
{
	filePaths.insert(dataGroupIndex, filePath);
	setFirstWrite(dataGroupIndex, true);
}
int MEMSDataSaver::getDataGroupCount()
{
	return dataGroupCount;
}
void MEMSDataSaver::setDataGroupCount(int dataGroupCount)
{
	this->dataGroupCount = dataGroupCount;
}
int MEMSDataSaver::getAccuracy()
{
	return accuracy;
}
void MEMSDataSaver::setAccuracy(int accuracy)
{
	this->accuracy = accuracy;
}
FileWriter* MEMSDataSaver::getFileWriter()
{
	return fileWriter;
}
void MEMSDataSaver::setFileWriter(FileWriter* fileWriter)
{
	this->fileWriter = fileWriter;
}
MEMSDataHandler* MEMSDataSaver::getMEMSDataHandler()
{
	return mEMSDataHandler;
}
void MEMSDataSaver::setMEMSDataHandler(MEMSDataHandler* mEMSDataHandler)
{
	if (Q_NULLPTR!=this->mEMSDataHandler)
	{
		disconnect(this->mEMSDataHandler, &MEMSDataHandler::signal_dataHandled, this, &MEMSDataSaver::slot_saveData);
	}
	this->mEMSDataHandler = mEMSDataHandler;
	connect(this->mEMSDataHandler, &MEMSDataHandler::signal_dataHandled, this, &MEMSDataSaver::slot_saveData);
}
LogSaver* MEMSDataSaver::getLogSaver()
{
	return logSaver;
}
void MEMSDataSaver::setLogSaver(LogSaver* logSaver)
{
	this->logSaver = logSaver;
}

